﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using Hongsh.Pay.Client.Proxy.Ssl;

namespace Hongsh.Pay.Client.Proxy.Http {
    // Token: 0x02000028 RID: 40
    public class HttpLisenter : IDisposable {
        // Token: 0x17000077 RID: 119
        // (get) Token: 0x06000124 RID: 292 RVA: 0x00002C75 File Offset: 0x00000E75
        public HttpServer Server { get; }

        // Token: 0x17000078 RID: 120
        // (get) Token: 0x06000125 RID: 293 RVA: 0x00002C7D File Offset: 0x00000E7D
        // (set) Token: 0x06000126 RID: 294 RVA: 0x00002C85 File Offset: 0x00000E85
        public Socket LisenterSocket { get; private set; }

        // Token: 0x17000079 RID: 121
        // (get) Token: 0x06000127 RID: 295 RVA: 0x00002C8E File Offset: 0x00000E8E
        public IPEndPoint EndPoint { get; }

        // Token: 0x1700007A RID: 122
        // (get) Token: 0x06000128 RID: 296 RVA: 0x00002C96 File Offset: 0x00000E96
        public bool IsListen {
            get {
                return LisenterSocket.IsBound;
            }
        }

        // Token: 0x06000129 RID: 297 RVA: 0x00002CA3 File Offset: 0x00000EA3
        public HttpLisenter(HttpServer server, IPEndPoint endPoint) : base() {
            //Class15.cwcZ3SWztHroi();
            //base..ctor();
            Server = server;
            EndPoint = endPoint;
        }

        // Token: 0x0600012A RID: 298 RVA: 0x0000B6B4 File Offset: 0x000098B4
        public void Listen() {
            if (LisenterSocket == null) {
                LisenterSocket = new Socket(EndPoint.AddressFamily, SocketType.Stream, ProtocolType.IP);
                LisenterSocket.Bind(EndPoint);
                LisenterSocket.Listen(128);
                SocketAsyncEventArgs socketAsyncEventArgs = new SocketAsyncEventArgs();
                socketAsyncEventArgs.Completed += AcceptSocket;
                LisenterSocket.AcceptAsync(socketAsyncEventArgs);
            }
        }

        // Token: 0x0600012B RID: 299 RVA: 0x00002CBE File Offset: 0x00000EBE
        public void Stop() {
            if (LisenterSocket != null) {
                LisenterSocket.Close();
                LisenterSocket = null;
            }
        }

        // Token: 0x0600012C RID: 300 RVA: 0x0000B72C File Offset: 0x0000992C
        private void AcceptSocket(object sender, SocketAsyncEventArgs e) {
            Socket acceptSocket = e.AcceptSocket;
            e.AcceptSocket = null;
            try {
                LisenterSocket.AcceptAsync(e);
            }
            catch {
            }
            try {
                acceptSocket.ReceiveTimeout = Server.ReadTimeOut;
                acceptSocket.SendTimeout = Server.WriteTimeOut;
                SslClientHello sslClientHello = null;
                Stream stream;
                if (Server.CertificateList.Count > 0 && (sslClientHello = acceptSocket.PeekClientHello()) != null) {
                    string value = Server.CertificateList.First().Value;
                    SslExtension host_ex;
                    if (sslClientHello.Extensions != null && sslClientHello.Extensions.TryGetValue("server_name", out host_ex)) {
                        value = Server.CertificateList.FirstOrDefault((KeyValuePair<string, string> d) => host_ex.Data.EndsWith(d.Key, StringComparison.OrdinalIgnoreCase)).Value;
                    }
                    stream = SSLStream.AuthenticateAsServer(acceptSocket, value);
                }
                else {
                    stream = new NetworkStream(acceptSocket);
                }
                if (stream == null) {
                    throw new Exception("网络套接字流读取失败！");
                }
                Server.OnHttpConnection(new HttpConnection(Server, acceptSocket, stream));
            }
            catch (Exception ex) {
                Console.WriteLine(ex.ToString());
            }
        }

        // Token: 0x0600012D RID: 301 RVA: 0x00002CDD File Offset: 0x00000EDD
        void IDisposable.Dispose() {
            LisenterSocket.Dispose();
        }
    }
}
